package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.informer.service.TagSearchService;
import com.metagurukul.informer.valueObjects.Questions;

/**
 * Servlet implementation class TagSearch
 */
@WebServlet("/TagSearch")
public class TagSearch extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TagSearch() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		try {
			int tagId = Integer.parseInt(request.getParameter("id"));
			TagSearchService tagSearch=new TagSearchService(tagId);
			List<Questions> questions= tagSearch.questionList();
			HttpSession session=request.getSession();
			session.setAttribute("pageName", "search");
			session.setAttribute("searchString", tagSearch.findTagName(tagId));
			session.setAttribute("listOfQuestions", questions);
			request.getRequestDispatcher("RetrieveQuestion.jsp").forward(request, response);
		}  catch (SQLException e) {
			e.printStackTrace(out);
			//request.getRequestDispatcher("Error.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
